package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.GroupBean;
import com.xuxin.nantai.bean.InspectorBean;
import com.xuxin.nantai.bean.SelectFlowingBean;
import com.xuxin.nantai.contract.SelectUpdateContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.SelectUpdatePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;


/**
 * Created by xu_xin
 * Created on 2020.06.12 0012
 */
public class SelectUpdateModel implements SelectUpdateContract.Model {
    @Override
    public void getInspector(SelectUpdatePresenter presenter, String loginName, String TeamType) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getSelectInspector(loginName,TeamType)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<InspectorBean>() {
            @Override
            public void onNext(InspectorBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getSelectFlowing(SelectUpdatePresenter presenter, String FlowingCode,String inspectorCode,String loginName) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getSelectFlowing(FlowingCode,inspectorCode,loginName)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<SelectFlowingBean>() {
            @Override
            public void onNext(SelectFlowingBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void getGroups(SelectUpdatePresenter presenter) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .getGroups()
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<GroupBean>() {
            @Override
            public void onNext(GroupBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void save(SelectUpdatePresenter presenter,
                     String LoginName,
                     String UserName,
                     String Guid,
                     String ID,
                     String team,
                     int reCheck,
                     String flowingCode,
                     String cInvName,
                     String cInvCode,
                     String packNum,
                     String comNum,
                     String percent,
                     String checkNum,
                     String area,
                     String groupName,
                     String groupCode,
                     String inspectorName,
                     String inspectorCode,
                     String inspectorTime,
                     String result,
                     String mark) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .updateSelect(LoginName,
                        UserName,
                        Guid,
                        ID,
                        team,
                        reCheck,
                        flowingCode,
                        cInvName,
                        cInvCode,
                        packNum,
                        comNum,
                        percent,
                        checkNum,
                        area,
                        groupName,
                        groupCode,
                        inspectorName,
                        inspectorCode,
                        inspectorTime,
                        result,
                        mark)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(BaseBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}